﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ComTools.Extensions
{

    /// <summary>
    /// 对象扩展类
    /// </summary>
    public static class ObjectExtensions
    {

        #region Object 的扩展

        /// <summary>
        /// 扩展方法：将对象转为字符串
        /// <para>
        /// 如果对象为空则返回默认值，默认值默认设置为空字符串
        /// </para>
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="defValue">默认值,默认为空字符串</param>
        /// <returns>对象的字符串值</returns>
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static string ToStringWithDefault(this object obj, string defValue = "")
        {
            if (obj == null) return defValue;
            return Convert.ToString(obj);
        }
        #endregion
    }
}
